<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="openSearch = true"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['output:cldReport:export']"
        >
          导出
        </el-button>
      </el-col>
    </el-row>

    <el-table
      highlight-current-row
      border
      stripe
      v-loading="loading"
      :data="tableData.rows"
      id="tabReport"
      @scrollBottom="load"
      :cell-style="iCellStyle"
      show-summary
      :summary-method="getSummaries"
      height="calc(100% - 100px)"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" type="index" align="center" />
      <!-- <el-table-column type="selection" width="50" align="center" /> -->
      <el-table-column
        label="制造单号"
        width="135"
        :show-overflow-tooltip="true"
        align="center"
        prop="cldZzdh"
      />
      <el-table-column
        label="计划工单数量"
        width="100"
        :show-overflow-tooltip="true"
        align="center"
        prop="modDdsl"
      />
      <!-- <el-table-column label="车间" width="80" :show-overflow-tooltip="true" prop="clmCj" /> -->
      <el-table-column
        label="领料物料代码"
        width="135"
        :show-overflow-tooltip="true"
        prop="cldWldm"
      />
      <el-table-column
        label="父项物料代码"
        width="100"
        :show-overflow-tooltip="true"
        align="center"
        prop="cldFxdm"
      />

      <el-table-column
        label="采购属性"
        width="100"
        :show-overflow-tooltip="true"
        align="center"
        prop="modCgsx"
      >
        <template #default="scope">
          <dict-tag :options="bomCgsxOptions" :value="scope.row.modCgsx" />
        </template>
      </el-table-column>
      <el-table-column
        label="单位"
        width="80"
        :show-overflow-tooltip="true"
        align="center"
        prop="itmDw"
      >
        <template #default="scope">
          <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
        </template>
      </el-table-column>
      <el-table-column
        label="储位信息"
        min-width="120"
        :show-overflow-tooltip="true"
        prop="cldCwdm"
      >
        <template #default="scope">
          <dict-tag :options="allCwOptions" :value="scope.row.cldCwdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="最小包装"
        width="100"
        :show-overflow-tooltip="true"
        align="center"
        prop="cldMinbz"
      />
      <el-table-column
        label="销售单号"
        width="135"
        :show-overflow-tooltip="true"
        align="center"
        prop="cldSodh"
      ></el-table-column>
      <el-table-column
        label="订单数量"
        width="80"
        :show-overflow-tooltip="true"
        align="center"
        prop="modTotalDdsl"
      />
      <el-table-column
        label="领料品名规格"
        min-width="250"
        fixed="right"
        :show-overflow-tooltip="true"
        prop="itmPmgg"
      />
      <el-table-column
        label="父项客户编码"
        min-width="100"
        fixed="right"
        :show-overflow-tooltip="true"
        prop="itmFxCustWldm"
      />
      <el-table-column
        label="父项品名规格"
        min-width="250"
        fixed="right"
        :show-overflow-tooltip="true"
        prop="itmFxPmgg"
      />
      <el-table-column
        label="总需求数量"
        width="90"
        fixed="right"
        :show-overflow-tooltip="true"
        align="center"
        prop="modXqsl"
      />
      <el-table-column
        label="工单需求数"
        width="90"
        fixed="right"
        :show-overflow-tooltip="true"
        align="center"
        prop="modLlsl"
      />
      <el-table-column
        label="已申请总数"
        width="90"
        fixed="right"
        :show-overflow-tooltip="true"
        align="center"
        prop="modYlsl"
      />
      <el-table-column
        label="待申请总数"
        v-if="typeChild != 'B'"
        width="100"
        fixed="right"
        :show-overflow-tooltip="true"
        align="center"
        prop="modDSqsl"
      />
      <el-table-column
        label="可用库存"
        width="75"
        fixed="right"
        :show-overflow-tooltip="true"
        align="center"
        prop="kcmKykc"
      />
    </el-table>

    <!-- 查询对话框   -->
    <el-dialog
      title="查询"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="650px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        label-width="75px"
        :rules="queryrules"
      >
        <el-col :span="12">
          <el-form-item label="时间范围">
            <el-date-picker
              v-model="dateRange"
              type="daterange"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
              clearable
              style="width: 100%"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="制造单号" prop="clmZzdh">
            <el-input v-model="queryParams.clmZzdh"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料代码" prop="modWldm">
            <el-input v-model="queryParams.modWldm"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="子项品名" prop="modZxPmgg">
            <el-input v-model="queryParams.modZxPmgg"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="父项代码" prop="modFxdm">
            <el-input v-model="queryParams.modFxdm"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="父项品名" prop="modFxPmgg">
            <el-input v-model="queryParams.modFxPmgg"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="仓库" prop="clmCkdm">
            <el-select
              size="small"
              style="width: 240px"
              v-model="queryParams.clmCkdm"
              placeholder="请选择仓库"
            >
              <el-option
                v-for="item in storageOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="综合查询" prop="integratedQuery">
            <el-input
              v-model="queryParams.integratedQuery"
              placeholder="综合查询"
              clearable
              size="small"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getAddCldByJZ, exportAddCldNonPlan } from "@/api/output/picking";
import { getAllCgList } from "@/api/project/mstr";
import { getAll, getstorageType } from "@/api/repertory/Storage";

export default {
  name: "CldReport",
  data() {
    return {
      /** 颜色事件 */
      iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
        if (row.somZtbz == "4") {
          return "color:#bb0331";
        } else if (row.somZtbz == "3") {
          return "color:#FF44AA";
        } else if (row.moeZtbz == "1") {
          return "color:#09aa46";
        }
      },
      // 遮罩层
      loading: true,
      exportLoading: false,
      openSearch: false,
      tableData: {
        rows: [],
        total: 0,
      },
      // 建立日期时间范围
      dateRange: [
        this.$moment().startOf("month").format(),
        this.$moment().endOf("month").format(),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 30,
        clmZzdh: null,
        clmCpDesc: null,
        integratedQuery: null,
        clmCj: "mp_000",
        clmDjbh: "CL2024051600002",
        clmCkdm: "12",
        clmType: "A",
        // params: {
        //   beginModJlrq: this.$moment().startOf('month').format(),
        //   endModJlrq: this.$moment().endOf('month').format(),
        // },
      },
      bomCgsxOptions: [],
      //有效仓库
      storageOptions: [],
    };
  },
  created() {
    //得到采购属性
    getAllCgList().then((response) => {
      this.bomCgsxOptions = response.data;
      for (let i of this.bomCgsxOptions) {
        i.dictLabel = i.cgName;
        i.dictValue = i.cgId + "";
        i.listClass = "default";
      }
    });
    //有效仓库
    getstorageType("0").then((response) => {
      this.storageOptions = response.data;

      for (let item of this.storageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });
    this.getList();
  },
  watch: {
    dateRange(val, old) {
      if (val && val[1]) {
        this.queryParams.params.beginModJlrq = val[0];
        this.queryParams.params.endModJlrq = val[1];
      } else {
        this.queryParams.params = {};
      }
    },
  },
  methods: {
    getList() {
      this.loading = true;
      this.queryParams.pageNum = 1;
      getAddCldByJZ(this.queryParams).then((response) => {
        this.tableData = response;
        this.loading = false;
      });
    },
    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.tableData.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        getAddCldByJZ(this.queryParams).then((response) => {
          this.tableData.rows = this.tableData.rows.concat(response.rows);
          this.tableData.total = response.total;
          this.loading = false;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.openSearch = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
      this.openSearch = false;
    },
    /** 导出按钮操作 */
    handleExport() {
      return exportAddCldNonPlan(this.queryParams).then((res) =>
        this.download(res.msg)
      );
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计:";
          return;
        }
        if (
          !["modXqsl", "modLlsl", "modYlsl", "modDSqsl"].includes(
            column.property
          ) ||
          !column.property
        ) {
          sums[index] = " ";
          return;
        }

        const values = data.map((item) => Number(item[column.property]));
        if (!values.every((value) => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return parseFloat((prev + curr).toFixed(2));
            } else {
              return prev;
            }
          }, 0);
        } else {
          sums[index] = "N/A";
        }
      });

      return sums;
    },
  },
};
</script>

<style lang="scss" scoped></style>
